Postgresql Docker Setup
Install Docker
- Linux
- Windows
- Mac
curl -fsSL https://get.docker.com | sh
sudo usermod -aG docker $USER
Log out and back in, then verify:
docker --version
- Download Docker Desktop for Windows
https://www.docker.com/products/docker-desktop/ - Install and enable WSL 2 when prompted, or manually install it:
- Right-click PowerShell and select Run as administrator
- Run:
wsl --install - Restart your machine after installation
For more details, see the official guide:
How to install Linux on Windows with WSL
- Restart and verify:
docker --version
Docker Desktop on Windows requires hardware virtualization to be enabled. It runs Linux containers inside a lightweight virtual machine (VM) that uses either Hyper-V (or the underlying Windows Hypervisor Platform) or the Windows Subsystem for Linux 2 (WSL 2), both of which depend on your CPU's hardware virtualization capabilities (Intel VT-x or AMD-V) being enabled in the BIOS/UEFI settings.
- Since windows home has no Hyper-v support it can't use vitualization, hence can't use docker. You can use (MAS tool)[https://massgrave.dev/] to change the version of your windows to include the feature.
- (Enable Virtualization on Windows)[https://support.microsoft.com/en-us/windows/enable-virtualization-on-windows-c5578302-6e43-4b4b-a449-8ced115f58e1]
- Download Docker Desktop for Mac
https://www.docker.com/products/docker-desktop/ - Install and launch Docker Desktop
- Verify:
docker --version
Postgres and Pgadmin docker compose
The following docker compose creates postgres 16 container on 5432 and pgadmin4 on port 5050. The default username and password for postgres database is postgres and postgres respectively.
Open Pgadmin4 by entering localhost:5050 or <remote_ip>:5050. Then use the following credential to connect to postgres database:
host: localhost or postgres or <inspect the docker container for private ip within the bridge>
username: postgres
password: postgres
port: 5432
services:
postgres:
image: postgres:16
container_name: local_postgres
restart: unless-stopped
environment:
POSTGRES_USER: postgres # change as you like
POSTGRES_PASSWORD: postgres # change as you like
POSTGRES_DB: postgres # initial DB
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U useName -d mydb || exit 1"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
networks:
- backend
pgadmin:
image: dpage/pgadmin4:latest
container_name: local_pgadmin
restart: unless-stopped
environment:
PGADMIN_DEFAULT_EMAIL: [email protected] # change
PGADMIN_DEFAULT_PASSWORD: ExampleAdmn123 # change
PGADMIN_CONFIG_SERVER_MODE: "False" # single-user desktop mode
volumes:
- ./pgadmin-data:/var/lib/pgadmin
# Also bind pgAdmin to a specific host IP
# For "localhost only" access:
ports:
- "5050:80"
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ["CMD", "wget", "-O", "-", "http://localhost:80/misc/ping"]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
networks:
- backend
networks:
backend:
driver: bridge
Connect to remote database using terminla
psql -h <remote_host> -p <port> -U <username> -d <target_db>
If you are using the above setup, the default port is 5432, username is postgres, target_db is postgres.
To restore a database on remote host use the following command,
pg_restore -h <remote_host> -U <username> -d <target_db_name> -v <path_to_dump_file>